package helperClassSearch;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class CreateUtilsHMOracle {

	public static void CreateUtilsClass(int[] sheetNumbers, String fileName, String packageName, String className) {

		File replacedFile = new File(fileName);
		replacedFile.delete();

		try {
			Workbook workbook = Workbook.getWorkbook(new File("C:\\Documents and Settings\\Administrator\\My Documents\\Dropbox\\CTA\\XLS\\CTA.xls"));
			FileWriter fstream = new FileWriter(fileName);
			BufferedWriter textFile = new BufferedWriter(fstream);

			textFile.write("package com.cyclingthealps." + packageName + ";\n");
			textFile.write("\n");
			textFile.write("import java.util.HashMap;\n");
			textFile.write("import com.cyclingthealps.client.dialogs.GoogleEarth;\n");
			textFile.write("import com.cyclingthealps.client.dialogs.Profile;\n");
			textFile.write("import com.cyclingthealps.client.planned.MyMessagesPlanned;\n");
			textFile.write("import com.cyclingthealps.client.protour.MyMessagesProTour;\n");
			textFile.write("import com.cyclingthealps.client.climbs.MyMessagesClimbs;\n");
			textFile.write("import com.google.gwt.core.client.GWT;\n");
			textFile.write("import com.google.gwt.maps.client.geom.LatLng;\n");
			textFile.write("import com.google.gwt.user.client.History;\n");
			textFile.write("import com.google.gwt.user.client.Window;\n");
			textFile.write("\n");
			textFile.write("			public class UtilsHMOracle {\n");
			textFile.write("public static MyMessages res = (MyMessages) GWT.create(MyMessages.class);\n");
			textFile.write("public static MyMessagesPlanned resPlanned = (MyMessagesPlanned) GWT.create(MyMessagesPlanned.class);\n");
			textFile.write("public static MyMessagesProTour resProTour = (MyMessagesProTour) GWT.create(MyMessagesProTour.class);\n");
			textFile.write("public static MyMessagesClimbs resClimbs = (MyMessagesClimbs) GWT.create(MyMessagesClimbs.class);\n");
			textFile.write("\n");

			textFile.write("protected static java.util.Map<String, String> passLinkByOracle = new HashMap<String, String>();\n");
			textFile.write("static {\n");

			for (int d = 0; d < sheetNumbers.length; d++) {
				Sheet sheet = workbook.getSheet(sheetNumbers[d]);
				Cell[] count = sheet.getColumn(0);
				String resources;
				if (sheetNumbers[d] == 0 || sheetNumbers[d] == 1 || sheetNumbers[d] == 2 || sheetNumbers[d] == 3) {
					resources = "res.";
				} else {
					if (sheetNumbers[d] == 11) {
						resources = "resProTour.";
					} else {
						if (sheetNumbers[d] == 17) {
							resources = "resClimbs.";
						} else {
							resources = "resPlanned.";
						}
					}

				}
				textFile.write("\n");
				for (int j = 1; j < count.length; j++) {
					Cell passOracleCell = sheet.getCell(40, j);
					String passOracle = passOracleCell.getContents();
					Cell passLinkCell = sheet.getCell(35, j);
					String passLinkEN = passLinkCell.getContents();
					textFile.write("passLinkByOracle.put(" + resources + passOracle + "(), \""+ passLinkEN + "\");\n");

				}


			}
			textFile.write("}\n");

//			textFile.write("protected static java.util.Map<String, String> passLinkByOracle = new HashMap<String, String>();\n");
//			textFile.write("static {\n");
//
//			for (int d = 0; d < sheetNumbers.length; d++) {
//				Sheet sheet = workbook.getSheet(sheetNumbers[d]);
//				Cell[] count = sheet.getColumn(0);
//				String resources;
//				if (sheetNumbers[d] == 0 || sheetNumbers[d] == 1 || sheetNumbers[d] == 2 || sheetNumbers[d] == 3) {
//					resources = "res.";
//				} else {
//					if (sheetNumbers[d] == 11) {
//						resources = "resProTour.";
//					} else {
//						if (sheetNumbers[d] == 17) {
//							resources = "resClimbs.";
//						} else {
//							resources = "resPlanned.";
//						}
//					}
//
//				}
//				textFile.write("\n");
//				for (int j = 1; j < count.length; j++) {
//					Cell passOracleCell = sheet.getCell(40, j);
//					String passOracle = passOracleCell.getContents();
//					Cell passLinkCell = sheet.getCell(35, j);
//					String passLinkEN = passLinkCell.getContents();
//					textFile.write("passLinkByOracle.put(" + resources + passOracle + "(), \""+ passLinkEN + "\");\n");
//
//				}
//
//			}
//			textFile.write("}\n");
			
			textFile.write("\n");
			textFile.write("public static void linksCTA(String historyToken) {\n");
			textFile.write("if (!historyToken.isEmpty()) {\n");
			textFile.write("if (historyToken.endsWith(\" 3D Tour\")) {\n");
			textFile.write("String linkPart = historyToken.substring(0, historyToken.length()-8);\n");
			textFile.write("History.newItem(passLinkByOracle.get(linkPart) + \"-tour\");\n");
			textFile.write("} else if (historyToken.endsWith(\" Streetview\")) {\n");
			textFile.write("String linkPart = historyToken.replace(\" Streetview\", \"\");\n");
			textFile.write("History.newItem(passLinkByOracle.get(linkPart) + \"-streetview\");\n");
			textFile.write("} else if (historyToken.endsWith(\" Profile\")) {\n");
			textFile.write("String linkPart = historyToken.substring(0, historyToken.length()-8);\n");
			textFile.write("History.newItem(passLinkByOracle.get(linkPart) + \"-profile\");\n");
			textFile.write("} else {\n");
			textFile.write("History.newItem(passLinkByOracle.get(historyToken));\n");
			textFile.write("}\n");
			textFile.write("}\n");
			textFile.write("}\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.close();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
